<template>
  <!-- 预约患者列表 -->
  <el-dialog
    :title="'预约患者（'+(curItem?curItem.dateDayStart:'')+'）'"
    :visible="value"
    width="1000px"
    custom-class="dialog-box"
    @close="onClose"
    @open="onOpen"
    :close-on-click-modal="false"
    :append-to-body="true"
  >
    <!-- <el-form :inline="true" @submit.native.prevent="getData(-1)">
      <el-form-item>
        <deptSearch></deptSearch>
      </el-form-item>
      <el-form-item>
        <doctorSearch></doctorSearch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" circle icon="el-icon-search" native-type="submit"></el-button>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="!qxBtns.edit" @click="onOpenEdit(null)">+添加</el-button>
      </el-form-item>
    </el-form>-->
    <table class="table-box">
      <thead>
        <tr>
          <th></th>
          <th>姓名</th>
          <th>年龄</th>
          <th>预约科室</th>
          <th>预约医生</th>
          <th>预约时间</th>
          <th>预约内容</th>
          <th>状态</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in tableList" :key="index">
          <td>
            <img
              v-if="!item.patientInfo.head"
              src="@/img/unknown-avatar.png"
              class="head-img"
              style="width:42px;height:42px;"
            />
            <img
              v-else
              :src="item.patientInfo.head|imageUrl"
              class="head-img"
              style="width:42px;height:42px;"
            />
          </td>
          <td>
            <el-link @click="onUrlPat(item)" type="primary">{{item.patientInfo.name}}</el-link>
          </td>
          <td>
            <span v-if="item.patientInfo.age>17">{{item.patientInfo.age}}岁</span>
            <span v-else>{{item.patientInfo.birthday|toTimeContrast}}</span>
            <!-- {{item.patientInfo.birthday}} -->
          </td>
          <td>{{ item.deptName}}</td>
          <td>{{ item.doctorName}}</td>
          <td>{{item.startTime}}~{{item.endTime}}</td>
          <td>
            <div style="max-width:250px">{{ item.registerContent}}</div>
          </td>
          <td>
            <span
              v-if="item.status==1"
              class="success-color"
            >{{item.status|changeAssist(statusEnum)}}</span>
            <span
              v-else-if="item.status==2"
              class="primary-color"
            >{{item.status|changeAssist(statusEnum)}}</span>
            <span
              v-else-if="item.status==4"
              class="error-color"
            >{{item.status|changeAssist(statusEnum)}}</span>
            <span v-else>{{item.status|changeAssist(statusEnum)}}</span>
          </td>
        </tr>
      </tbody>
    </table>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br />
    <pager v-model="pager" @change-pagenum="getData">
      <el-button :loading="btnLoading" @click="onExcel">导出Excel</el-button>
    </pager>
  </el-dialog>
</template>
<script>
import excel from "@/_js/excel.js";
export default {
  props: ["curItem", "value"],
  components: {},
  data() {
    return {
      pager: { pageSize: 15, pageNum: 1, total: 0 },
      tableList: [],
      statusEnum: [],
      btnLoading: false,
    };
  },
  created() {
    let th = this;
    th.getCommonConfigByKey("预约状态").then((res) => {
      th.statusEnum = res;
    });
  },
  methods: {
    //导出
    onExcel() {
      let th = this;
      let j = {
        status: "1,2,3,5,6",
        pageSize: 2000,
        pageNum: 1,
      };
      Object.assign(j, th.curItem);
      let data = [];
      let fn = function (arr) {
        data = arr.map((item) => {
          let j = {
            name: item.patientInfo.name,
            age: item.patientInfo.age,
            deptName: item.deptName,
            doctorName: item.doctorName,
            time: item.startTime + "~" + item.endTime,
            registerContent: item.registerContent,
            status: item.status,
          };
          return j;
        });
        let title = [
          { key: "name", name: "姓名" },
          { key: "age", name: "年龄" },
          {
            key: "deptName",
            name: "预约科室",
          },
          {
            key: "doctorName",
            name: "预约医生",
          },
          {
            key: "time",
            name: "预约时间",
          },
          { key: "registerContent", name: "预约内容" },
        ];
        let statusTitle = {
          key: "status",
          name: "状态",
          type: "enum",
          enum: {},
        };
        th.statusEnum.forEach((item) => {
          statusTitle.enum[item.id] = item.desc;
        });
        title.push(statusTitle);
        excel(data, title, `${th.curItem.dateDayStart}预约患者数据`);
      };
      th.btnLoading = true;
      th.$api
        .getRegisterListInfo(j)
        .then((res) => {
          if (res.executed) {
            fn(res.dataBody.list);
          }
          th.btnLoading = false;
        })
        .catch((res) => {
          th.btnLoading = false;
        });
    },
    getData(type) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      let j = {
        status: "1,2,3,5,6",
      };
      Object.assign(j, th.pager, th.curItem, { isDateDaySort: 1 });
      th.$api.getRegisterListInfo(j).then((res) => {
        if (res.executed) {
          th.tableList = res.dataBody.list;
          th.pager.total = res.dataBody.total;
        }
      });
    },
    //跳转病人库
    onUrlPat(obj) {
      const th = this;
      let url = "/page/userDetail";
      let j = th.getUrlToken({
        id: th.jiaMi(obj.patientInfo.patientId),
      });
      th.addNavMenu("病人库", url + th.$utils.JsonToUrl(j));
      th.$router.push({
        path: url,
        query: j,
      });
    },
    onOpen() {
      let th = this;
      th.$nextTick(function () {
        th.getData(-1);
      });
    },
    onClose() {
      this.tableList = [];
      this.$emit("input", false);
    },
  },
};
</script>
